/**
 * Created by zhanglihe on 15/10/30.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'template',
    'collection',
    'model',
    'dialog'
], function ($, common, ready, template, Collection, Model, dialog) {
    var other_file = 0;
    var index = function () {
        this.init();
    };
    index.prototype = {
        init: function () {
            var _this = this;

            //地区
            $.ajax({
                url: _global.url.api + 'regions',
                type: 'get',
                async: false,
                data: {depth: 2},
                success: function (res) {
                    if (res.code == 0) {
                        _this.set('region', res.data);
                    }
                }
            });
            loading();
            this.event();
            this.fetch();
        },

        fetch: function (_args) {
            var args = _args || {};
            args.limit = 10;
            args.offset = 0;
            var _this = this;
            this.collection = new Collection({
                container: 'data-list',
                template: 'data-item',
                url: _global.url.api + 'agents/apply',
                condition: args,
                callback: function () {
                    loaded();
                }
            });
            this.collection.fetch();
        },
        set: function ($k, $v) {
            this[$k] = $v;
        },
        event: function () {
            var _this = this;

            /**
             * 增加代理商
             */
            $('.add-apply').on('click', function (e) {
                e.preventDefault();

                dialog({
                    title: '增加代理商',
                    width: 750,
                    content: template('add-tpl', {region: _this.region}),
                    ok: function () {
                        var data = {};
                        data.name = $('input[name="name"]').val();
                        data.contact = $('input[name="contact"]').val();
                        data.mobile = $('input[name="mobile"]').val();
                        data.tel = $('input[name="tel"]').val();
                        data.province = $('[name="province"]').val();
                        data.city = $('[name="city"]').val();
                        data.other_file = other_file;
                        if (new Model({
                                url: _global.url.api + 'agent/apply',
                                data: data
                            }).post()) {
                            _this.collection.fetch();
                            return true;
                        }
                        return false;
                    }, cancel: function () {
                    }
                }).show();
            });
            //地区联动
            $('body').on('change', '.province', function () {
                $('select[name="city"]').html('');
                var id = $(this).val();
                var city = _this.region[id].child;
                for (var n in city) {
                    $('select[name="city"]').append('<option value="' + city[n].id + '">' + city[n].name + '</option>')
                }
            });

            $('input,select,textarea').on('change', function (e) {
                _this.change(e);
            });

            //上传附件
            $('body').on('click', '#btn-upload', function (e) {
                e.preventDefault();
                _this.upload_dialog = dialog({
                    content: template('upload-tpl', {}),
                    width: 550,
                    ok: function () {
                        var affix = $("#upload-frame")[0].contentWindow.affix.get();
                        _this.affixPreview(affix);
                    }, cancel: function () {
                    }
                });
                _this.upload_dialog.show();
            });


            /**
             * 搜索事件
             */
            $('#btn-search').on('click', function (e) {
                e.preventDefault();
                _this.collection.condition.keyword = $('input[name="keyword"]').val();
                _this.collection.reload();
            });

            /**
             * 按状态查询
             */
            $('#btn-status').find('a').on('click', function (e) {
                e.preventDefault();
                _this.collection.condition.keyword = '';
                _this.collection.condition.status = $(e.target).attr('rel');
                _this.collection.reload();
                $(e.target).addClass('active').siblings().removeClass('active');
            });
        },

        change: function (e) {
            this.data[$(e.target).attr('name')] = $(e.target).val();
        },
        affixPreview: function (affix) {
            console.log('this.data', affix);
            if (typeof affix.id !== 'undefined') {
                other_file = affix.id;
                $('#img-view').html(template('affix-tpl', affix));
            }
        }

    };

    new index();

});